package cn.com.zero.common.log.dubbo;

import cn.com.zero.api.log.dubbo.ILogService;
import cn.com.zero.api.log.vo.LogVO;
import cn.com.zero.base.service.BaseService;
import cn.com.zero.common.log.mapper.ILogMapper;
import cn.com.zero.common.log.po.LogPO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author xiongxj
 * @version 1.0.0
 * @Description 日志service实现类
 * @createTime 2023/2/9 15:04
 */
@Component
@DubboService
public class LogServiceDubbo extends BaseService implements ILogService {

    private ILogMapper logMapper;

    @Async
    @Override
    public void saveLog(LogVO logVO) {
        try {
            logger.trace("日志准备入库：{}", logVO.toString());
            LogPO logPO = new LogPO();
            logPO.setModuleName(logVO.getModuleName());
            logPO.setUserId(logVO.getUserId());
            logPO.setCatalogId(logVO.getCatalogType());
            logPO.setIpAddress(logVO.getIpAddress());
            logPO.setRequestUrl(logVO.getRequestUrl());
            logPO.setRequestMethod(logVO.getRequestMethod());
            logPO.setMethodName(logVO.getMethodName());
            logPO.setRequestParameters(logVO.getRequestParameters());
            logPO.setRequestBody(logVO.getRequestBody());
            logPO.setContent(logVO.getContent());
            logPO.setStatus(logVO.getStatus());
            logPO.setResponseContent(logVO.getResponseContent());
            logPO.setErrorContent(logVO.getErrorContent());
            logPO.setCostTime(logVO.getCostTime());
            logPO.setRemark(logVO.getRemark());
            logMapper.addLog(logPO);
            logger.debug("日志入库成功：{}", logPO.toString());
        } catch (Exception e) {
            logger.warn("日志入库失败：{}，发生异常：{}", logVO.toString(), e);
        }
    }

    @Autowired
    public void setLogMapper(ILogMapper logMapper) {
        this.logMapper = logMapper;
    }
}
